<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','salecate','mobile','realname','status','jointime','accounttime','merchname');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_tour_merch',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$v['jointime']=date('Y/m/d H:i:s',$v['jointime']);
		$v['url']=custom_url('jy_tour/merch.shanghu//id='.$v['id'],true);
		$v['accounttime']=date('Y/m/d H:i:s',$v['accounttime']);
		
	}
	$return['status']=array('0'=>array('id'=>0,'title'=>'待入驻'),'1'=>array('id'=>1,'title'=>'入驻中'),'2'=>array('id'=>2,'title'=>'暂停中'));
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'多商户管理'),
			array('op'=>'edit','title'=>'添加多商户'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'salecate','title'=>'主营项目','type'=>'string'),								
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
									
			array('name'=>'jointime','title'=>'入驻时间','type'=>'string'),
			array('name'=>'accounttime','title'=>'到期时间','type'=>'string'),								
		
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status2'),	
			array('name'=>'url','title'=>'入口','type'=>'ewm'),						
			
		),
		
		),
		'search'=>array(
			array('name'=>'merchname','width'=>15,'title'=>'商户名称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'电话','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status'),
		)
	);
	$return['status2']=array('0'=>'待审核','1'=>'入驻中','2'=>'暂停中',);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_tour_merch',array('id'=>$id));
	$item['lat_lng']=array('lat'=>$item['lat'],'lng'=>$item['lng']);
	}else{
		$item=array();
	}
	$item['upass']='';
    $return['item']=$item;
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'多商户设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'多商户管理'),
			array('op'=>'edit','title'=>'添加多商户'),
		),
		'edit'=>array(
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'logo','title'=>'商户logo','type'=>'image'),
			array('name'=>'salecate','title'=>'主营项目','type'=>'string'),	
			array('name'=>'cateid','title'=>'商户分类','type'=>'select','options'=>'cate','option_title'=>'catename'),
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'uname','title'=>'账户名','type'=>'string'),
			array('name'=>'upass','title'=>'账户密码','type'=>'string'),
			array('name'=>'mid','title'=>'选择管理员','type'=>'choose_title','action'=>'get_member'),					
			array('name'=>'desc','title'=>'商户简介','type'=>'string'),
			array('name'=>'address','title'=>'商户地址','type'=>'string'),	
			array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'tel','title'=>'商户电话','type'=>'string'),	
			array('name'=>'isrecommand','title'=>'是否推荐','type'=>'radio','options'=>'status2'),			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		//	array('name'=>'groupid','title'=>'商户组','type'=>'select','options'=>'group','option_title'=>'groupname'),	
			array('name'=>'payrate','title'=>'抽成利率（%）','type'=>'string'),
			array('name'=>'payopenid','title'=>'选择收款人','type'=>'choose_title','action'=>'get_member'),					
			)
			);
	$return['cate']=pdo_getall('jy_tour_merch_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
//	$return['group']=pdo_getall('jy_tour_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'));
	$return['status']=array('1'=>'入驻中','2'=>'暂停中',);
	$return['status2']=array('1'=>'是','0'=>'否');
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['merchname'] || !$post['mid'] ||  !$post['uname']){
		exi('商户名称管理员和账户必填','error');
	}
	
	$post['upass']=base64_encode($post['upass']);
	$post['lat']=$post['lat_lng']['lat'];
	$post['lng']=$post['lat_lng']['lng'];
	unset($post['lat_lng']);
	$info=pdo_get('jy_tour_merch',array('mid'=>$post['mid'],'uniacid'=>$_SESSION['uniacid']));
	
	$uname=pdo_get('jy_tour_merch',array('uname'=>$post['uname']));
	
	if($post['id']){
		if(!$post['upass']){
			unset($post['upass']);
		}
		if($info && $info['id']!=$post['id']){
			exi('一个管理员只能绑定一个账号','error');
		}
		if($uname && $uname['id']!=$post['id']){
		exi('登陆账户名不可以重复','error');	
		}
		pdo_update('jy_tour_merch',$post,array('id'=>$post['id']));
	}else{
		if($uname){
		exi('登陆账户名不可以重复','error');	
	}
		if($info){
			exi('一个管理员只能绑定一个账号','error');
		}
		$post['uniacid']=$_SESSION['uniacid'];
		$post['jointime']=time();
		pdo_insert('jy_tour_merch',$post);
	}
	
	exi('保存成功','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_tour_merch',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_tour_merch',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>